<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        canvas {
            width: 500px;
            height: 300px;
            border: 1px solid #000;
        }
    </style>
</head>

<body>
    <div>
        <canvas></canvas>
        <button>按钮</button>
    </div>
    <script>
        var canvas = document.querySelector("canvas");
        // 获取一个绘画环境对象
        var can = canvas.getContext("2d");
        can.strokeStyle = "red"; //描边颜色
        can.lineWidth = "2px"; //描边宽度
        // 画板比例
        can.scale(0.6, 0.5);
        canvas.onmousedown = function(){
            can.beginPath();
            canvas.onmousemove = function(e){
                can.lineTo(e.clientX,e.clientY);
                can.stroke();
            }
        }
        document.body.onmouseup = function(){
            canvas.onmousemove = null;
        }
        var but = document.querySelector("button");
        but.onclick = function(){
            var url = canvas.toDataURL("image/jpg");
            // console.log(url);
            window.open(url)
        }

    </script>
</body>

</html>